﻿package com.hh.bss.auth.client;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hh.bss.common.cache.SystemRoleResourceCache;
import com.hh.bss.common.cache.SystemUserCache;
import com.hh.bss.config.Environment;
import com.hh.bss.util.HTTPHelper;
import com.hh.bss.util.StringHelper;

public class OnlineManager {

	private HttpServletRequest request = null;
	private HttpServletResponse response = null;

	public OnlineManager(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}

	/**
	 * 写cookie信息，浏览器关闭前有效
	 * 
	 * @param name
	 *            String : cookie名称
	 * @param value
	 *            String : cookie值
	 */
	public void addCookie(String name, String value) {

		// 如果有多个域名则设置多个cookie
		if (StringHelper.isEmpty(Environment.DOMAIN_NAME))
			return;
		String[] domains = Environment.DOMAIN_NAME.split(",");
		for (int i = 0; i < domains.length; i++) {
			String domain = domains[i];
			Cookie cookie = new Cookie(name, value);
			cookie.setMaxAge(30 * 24 * 60 * 60); // 一周
			cookie.setDomain(domain);
			cookie.setPath("/");
			this.response.addCookie(cookie);

		}

	}

	/**
	 * 清除cookie信息
	 * 
	 * @param name
	 *            String : 待清除的cookie名称
	 */
	public void removeCookie(String name) {
		// 清除COOKIE
		// 如果有多个域名则删除多个cookie
		if (StringHelper.isEmpty(Environment.DOMAIN_NAME))
			return;

		String[] domains = Environment.DOMAIN_NAME.split(",");
		for (int i = 0; i < domains.length; i++) {
			String domain = domains[i];
			Cookie cookie = new Cookie(name, "");
			cookie.setDomain(domain);
			cookie.setPath("/");
			cookie.setMaxAge(0);
			this.response.addCookie(cookie);
		}

	}

	public void signIn(OnlineUser onlineUser) {
		if (onlineUser == null)
			return;
		String value = OnlineUser.encode(onlineUser);
		addCookie(Environment.ONLINE_COOKIE_NAME, value);
	}

	public OnlineUser getOnlineUser() {

		Cookie cookie = HTTPHelper.getCookie(this.request, Environment.ONLINE_COOKIE_NAME);
		String value = (cookie == null) ? null : cookie.getValue();
		if (StringHelper.isEmpty(value))
			return null;
		return OnlineUser.decode(value);
	}

	public boolean isSignIn() {
		return !(getOnlineUser() == null);
	}

	public void signOut() {
		removeCookie(Environment.ONLINE_COOKIE_NAME);
	}

	/**
	 * 只处理URI，不考虑参数
	 * 
	 * @return boolean : 如果有权限返回true，否则返回false
	 */
	public boolean hasPermission() {
		String pageURI = request.getRequestURI().replaceFirst(request.getContextPath(), "");
		return hasPermission(pageURI);
	}

	public String getResourceName() {
		String pageURI = request.getRequestURI().replaceFirst(request.getContextPath(), "");

		return SystemRoleResourceCache.getSystemResourceNameURL(pageURI);
		// return hasPermission(pageURI);
	}

	public boolean hasPermission(String pageURI) {
		// if (true) return false;
		// System.out.println("pageURI:"+pageURI);
		// 有定义的才做控制
		OnlineUser onlineUser = getOnlineUser();

		// 查询用户角色

		// 用户角色权限
		boolean isAuthURL = SystemRoleResourceCache.isAuthURL(pageURI);
		if (!isAuthURL)
			return true;
		System.out.println("pageURI:" + pageURI + "--" + isAuthURL);
		return SystemUserCache.isUerAuth(onlineUser.getId(), pageURI);
	}

	public boolean hasPermission(String pageURI, OnlineUser onlineUser) {
		// 用户角色权限
		boolean isAuthURL = SystemRoleResourceCache.isAuthURL(pageURI);
		if (!isAuthURL)
			return true;
		System.out.println("pageURI:" + pageURI + "--" + isAuthURL);
		return SystemUserCache.isUerAuth(onlineUser.getId(), pageURI);
	}
}
